//
//  AppDelegate.m
//  ZAKER
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "MovieViewController.h"

@interface AppDelegate ()

@property (nonatomic, strong) AFNetworkReachabilityManager *manager;
@property (nonatomic, strong) ZWIntroductionViewController *zwViewController;
@property (nonatomic, retain) UITabBarController *tabBar;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
     [NSThread sleepForTimeInterval:1.0];
    [UMSocialData setAppKey:@"55dbb3bfe0f55a5d82006783"];
    
    self.window = [[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
     self.tabBar = [[UITabBarController alloc]init];
    
    self.manager = [AFNetworkReachabilityManager sharedManager];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkChange) name:AFNetworkingReachabilityDidChangeNotification object:nil];
    [_manager startMonitoring];
    
    
    
    HomePageViewController *home = [[[HomePageViewController alloc]init] autorelease];
    UINavigationController *homeNavi = [[[UINavigationController alloc]initWithRootViewController:home] autorelease];
    UIImage *image = [UIImage imageNamed:@"t11.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    homeNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"稀奇" image:[UIImage imageNamed:@"t1.png"] selectedImage:image];
    homeNavi.tabBarController.tabBar.barTintColor = [UIColor redColor];
    
    SubscriptionViewController *sub = [[SubscriptionViewController alloc]init];
    UINavigationController *subNavi = [[UINavigationController alloc]initWithRootViewController:sub];
    UIImage *subImage = [UIImage imageNamed:@"t21.png"];
    subImage = [subImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    subNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"订阅" image:[UIImage imageNamed:@"t2.png"] selectedImage:subImage];
    
    MovieViewController *hot = [[[MovieViewController alloc]init] autorelease];
    UINavigationController *hotNavi = [[[UINavigationController alloc]initWithRootViewController:hot] autorelease];
    UIImage *hotImage = [UIImage imageNamed:@"t41.png"];
    hotImage = [hotImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    hotNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"视频" image:[UIImage imageNamed:@"t4.png"] selectedImage:hotImage];
    
    EntertainmentViewController *enter = [[EntertainmentViewController alloc]init];
    UINavigationController *enterNavi = [[UINavigationController alloc]initWithRootViewController:enter];
    UIImage *enterImage = [UIImage imageNamed:@"t31.png"];
    enterImage = [enterImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    enterNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"玩乐" image:[UIImage imageNamed:@"t3.png"] selectedImage:enterImage];
    
    MySetViewController *mySet = [[MySetViewController alloc]init];
    UINavigationController *mySetNavi = [[UINavigationController alloc]initWithRootViewController:mySet];
    UIImage *myImage = [UIImage imageNamed:@"my1.png"];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    mySetNavi.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"my.png"] selectedImage:myImage];
    
    [[UINavigationBar appearance]setBarTintColor:[UIColor grayColor]];
    
   
    [_tabBar setViewControllers:@[homeNavi, subNavi, hotNavi, enterNavi, mySetNavi]];
    _tabBar.tabBar.tintColor = [UIColor redColor];
      [_tabBar release];
//    [homeNavi release];
    [subNavi release];
//    [hotNavi release];
    [enterNavi release];
//    [home release];
    [sub release];
//    [hot release];
    [enter release];
    [mySetNavi release];
    [mySet release];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if ([user objectForKey:@"firstLoad"] == nil) {
        [self createFirstPageView];
        [user setValue:@"yes" forKey:@"firstLoad"];
    } else {
        _window.rootViewController  = self.tabBar;
        
    }
    
    
    return YES;
}
- (void)netWorkChange
{
    
    // 获取当前网络状态
    AFNetworkReachabilityStatus status = _manager.networkReachabilityStatus;
    
    // 没有网络
    if (status == AFNetworkReachabilityStatusUnknown || status == AFNetworkReachabilityStatusNotReachable) {
        UIAlertView *noNetAlert = [[UIAlertView alloc] initWithTitle:@"当前没有网络" message:nil delegate:self cancelButtonTitle:@"我知道了,我是故意的" otherButtonTitles:nil];
        [noNetAlert show];
    }
}

- (void)createFirstPageView
{
    // 放在前层的图片
    NSArray *brontArray = @[@"", @"", @""]; // 可以放一些镂空的文字
    NSArray *backImageArray = @[@"y1.jpg", @"y2.jpg", @"y3.jpg"];
    self.zwViewController = [[ZWIntroductionViewController alloc] initWithCoverImageNames:brontArray backgroundImageNames:backImageArray];
    
    // 把引导页加在window上
    [self.window addSubview:_zwViewController.view];
    
    AppDelegate *my = self;
    self.zwViewController.didSelectedEnter = ^() {
        // 很重要
        [my.zwViewController.view removeFromSuperview];
        my.zwViewController = nil;
        // 给window个你的rootViewController
        _window.rootViewController  = self.tabBar;
    };
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
